package chat

import (
	"fmt"
	base "gxcvu-ai/app/controller/Base"
	chatModel "gxcvu-ai/app/model/chat"
	"gxcvu-ai/pkg/auth"
	"gxcvu-ai/pkg/chat"
	"gxcvu-ai/pkg/response"
	"gxcvu-ai/validate"

	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

type ChatController struct {
	base.BaseController
}

func (cc *ChatController) Create(c *gin.Context) {
	request := validate.ChatForm{}

	if ok := validate.Validate(c, &request, validate.ValidateCreateChat); !ok {
		return
	}

	chatObj := chatModel.CreateChat(chatModel.Chat{
		UserID: cast.ToUint(auth.CurrentUID(c)),
		Title:  request.Title,
	})

	response.CreatedJSON(c, chatObj)
}

func (cc *ChatController) Edit(c *gin.Context) {
	request := validate.ChatForm{}

	if ok := validate.Validate(c, &request, validate.ValidateCreateChat); !ok {
		return
	}

	chatObj, _ := chatModel.GetChat(cast.ToString(request.ChatID))
	chatObj.Title = request.Title
	chatObj.Save()
	response.Success(c)

}

func (cc *ChatController) Get(c *gin.Context) {
	request := validate.GetChatForm{}

	if ok := validate.Validate(c, &request, validate.ValidateGetChat); !ok {
		return
	}

	resp, _ := chatModel.GetChat(cast.ToString(request.ChatID))

	response.Data(c, resp)
}

func (cc *ChatController) Chat(c *gin.Context) {
	request := validate.ChatForm{}

	if ok := validate.Validate(c, &request, validate.ValidateChat); !ok {
		return
	}

	var chatEng chat.ChatEngine
	switch request.Type {
	case "qianfan":
		chatEng = chat.QianFan{}
	case "gpt3":
		chatEng = chat.Openai{
			Model: "gpt-3.5-turbo",
		}
	case "gpt4":
		chatEng = chat.Openai{
			Model: "gpt-4",
		}
	case "kimi":
		chatEng = chat.Kimi{}
	case "tongyi":
		chatEng = chat.Tongyi{}
	default:
		fmt.Println("未知的聊天引擎类型: ", request.Type)
		chatEng = chat.Tongyi{}
	}

	// !--------------以下废弃，采用客户端传回来的数据进行组包------------!
	// // 组包新数据
	// message := chatModel.Message{
	// 	UserID:  cast.ToUint(auth.CurrentUID(c)),
	// 	ChatID:  request.ChatID,
	// 	Role:    "user",
	// 	Content: request.Content,
	// }

	// // 处理聊天数据
	// // 获取所有的聊天数据
	// data := chatModel.GetMsg(auth.CurrentUID(c), request.ChatID)
	// data = append(data, message)
	// !--------------以上废弃，采用客户端传回来的数据进行组包------------!
	data := []chatModel.Message{}
	for _, v := range request.Messages {
		data = append(data, chatModel.Message{
			UserID:  cast.ToUint(auth.CurrentUID(c)),
			ChatID:  request.ChatID,
			Role:    v.Role,
			Content: v.Content,
		})
	}

	fmt.Println(data)

	//创建一个SSE服务
	c.Header("Content-Type", "text/event-stream")
	c.Header("Cache-Control", "no-cache")
	c.Header("Connection", "keep-alive")
	// 发送初始消息
	c.SSEvent("init", "Connected to server")

	chatEng.NewChat(data, c)
}

func (cc *ChatController) Resend(c *gin.Context) {
	request := validate.ChatForm{}

	if ok := validate.Validate(c, &request, validate.ValidateChat); !ok {
		return
	}

	var chatEng chat.ChatEngine
	switch request.Type {
	case "qianfan":
		chatEng = chat.QianFan{}
	case "gpt3":
		chatEng = chat.Openai{
			Model: "gpt-3.5-turbo",
		}
	case "gpt4":
		chatEng = chat.Openai{
			Model: "gpt-4",
		}
	case "kimi":
		chatEng = chat.Kimi{}
	case "tongyi":
		chatEng = chat.Tongyi{}
	default:
		fmt.Println("未知的聊天引擎类型: ", request.Type)
		chatEng = chat.QianFan{}
	}

	// 处理聊天数据
	// 获取所有的聊天数据
	data := chatModel.GetMsg(auth.CurrentUID(c), request.ChatID)
	chatEng.NewChat(data, c)
}

func (cc *ChatController) GetAllChat(c *gin.Context) {
	chatList, _ := chatModel.GetAllChat(auth.CurrentUID(c))
	response.Data(c, chatList)
}
